--[[
    author:Lee
    time:2021-07-09 16:56:24
]]
-- 工具函数

local log        = require("modules.commhm.log");

local os            = os
local ngx           = ngx
local type          = type
local math          = math
local tonumber      = tonumber

--------------------------------------------------------------
local FOO = {

}


--获取日0点
FOO.get_today_zero_time = function(time)
    time = time or os.time()
    local now_date = os.date("*t", time)
    local Today_Time = os.time{year=now_date.year, month=now_date.month, day=now_date.day, hour=0,min=0,sec=0}
    return Today_Time
end


--! @brief 获取周0点
--! @param time时间戳
--! @return 周零点时间戳
FOO.get_week_zero_time = function(time)
    time = time or os.time()
    local today = tonumber(os.date("%w", time))  --周几
    today = (today == 0) and 7 or today          -- today = 0 为周日， 周日按第7天算
    local today_zero = FOO.get_today_zero_time(time) --今日0点
    local weekly_zero_time = today_zero - ((today-1)*86400)
    return math.floor(weekly_zero_time)
end


--获取月一日0点
FOO.get_month_zero_time = function(time)
    time = time or os.time()
    local now_date = os.date("*t", time)
    local Today_Time = os.time{year=now_date.year, month=now_date.month, day=1, hour=0,min=0,sec=0}
    return Today_Time
end


--比较两个时间是否是同一天
FOO.is_same_day = function(time1, time2)
    local day1 = FOO.get_today_zero_time(time1)
    local day2 = FOO.get_today_zero_time(time2)
    return day1 == day2
end


--比较两个时间是否是同一周
FOO.is_same_week = function(time1, time2)
    local week1 = FOO.get_week_zero_time(time1)
    local week2 = FOO.get_week_zero_time(time2)
    return week1 == week2
end


--比较两个时间是否是同一月
FOO.is_same_month = function(time1, time2)
    local month1 = FOO.get_month_zero_time(time1)
    local month2 = FOO.get_month_zero_time(time2)
    return month1 == month2
end


-------------------------------------------------------------------------------------------------------------
-- 接口函数	: set_nginx_share
-- 说明		: 设置共享内存
-- 参数  	: cache_name	缓存名
-- 参数  	: key			键
--			: value			值
--			: exptime		过期时间
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.set_nginx_share = function(cache_name, key, value, exptime)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_welfare","set_nginx_share:cache_name not a string", tostring(cache_name))
		return false
	end

	if not key or not value then
		log.day_list("business_welfare","set_nginx_share:key or value is nil ", tostring(cache_name), tostring(key), tostring(value))
		return false
	end

	if type(key) ~= "string" then
		log.day_list("business_welfare","set_nginx_share:key not a string", tostring(cache_name), type(key))
		return false
	end

	if type(value) ~= "table" then
		log.day_list("business_welfare","set_nginx_share:value not a table", tostring(cache_name), type(value))
		return false
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_welfare","set_nginx_share:cache_data not exist", tostring(cache_name), tostring(key))
		return false
	end

	cache_data:set(key, table.tostring(value), tonumber(exptime) or 0)
end


-------------------------------------------------------------------------------------------------------------
-- 接口函数	: get_nginx_share
-- 说明		: 获取共享内存
-- 参数  	: cache_name	缓存名
-- 参数  	: key			键
-- 返回值	: 返回key对应的共享内存数据
-------------------------------------------------------------------------------------------------------------
FOO.get_nginx_share = function(cache_name, key)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_welfare","get_nginx_share:cache_name not a string", tostring(cache_name))
		return {}
	end

	if not key or type(key) ~= "string" then
		log.day_list("business_welfare","get_nginx_share:key not a string", tostring(cache_name), type(key))
		return {}
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_welfare","get_nginx_share:cache_data not exist", tostring(cache_name), tostring(key))
		return {}
	end

	local cache_string = cache_data:get(key)
	if not cache_string then
		return {}
	end

	if type(cache_string) ~= "string" then
		log.day_list("business_welfare","get_nginx_share:cache_string not a string", tostring(cache_name), type(cache_string))
		return {}
	end

	local cache_table = table.loadstring(cache_string)
	if not cache_table or type(cache_table) ~= "table" then
		log.day_list("business_welfare","get_nginx_share:cache_data not a table", tostring(cache_name), type(cache_data))
		return {}
	end

	return cache_table
end


-------------------------------------------------------------------------------------------------------------
-- 接口函数	: del_nginx_share
-- 说明		: 删除共享内存
-- 参数  	: cache_name	缓存名
-- 参数  	: key			键
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.del_nginx_share = function(cache_name, key)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_welfare","del_nginx_share:cache_name not a string", tostring(cache_name))
		return {}
	end

	if not key or type(key) ~= "string" then
		log.day_list("business_welfare","del_nginx_share:key not a string", type(key))
		return
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_welfare","del_nginx_share:cache_data not exist", tostring(cache_name), tostring(key))
		return {}
	end

	cache_data:delete(key)
end


-------------------------------------------------------------------------------------------------------------
-- 接口函数	: clear_nginx_share
-- 说明		: 清除共享内存
-- 参数  	: cache_name	缓存名
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.clear_nginx_share = function(cache_name)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_welfare","clear_nginx_share:cache_name not a string", tostring(cache_name))
		return
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_welfare","clear_nginx_share:cache_data not exist", tostring(cache_name))
		return
	end

	cache_data:flush_all()
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: flush_nginx_share
-- 说明		: 清除过期共享内存
-- 参数  	: cache_name	缓存名
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.flush_nginx_share = function(cache_name)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_welfare","flush_nginx_share:cache_name not a string", tostring(cache_name))
		return
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_welfare","flush_nginx_share:cache_data not exist", tostring(cache_name))
		return
	end

	cache_data:flush_expired()
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: get_keys_share
-- 说明		: 获取共享内存所有key
-- 参数  	: cache_name	缓存名
-- 参数  	: max_count			最大数量
-- 返回值	: 无
----------------------------------------------
FOO.get_keys_share = function(cache_name, max_count)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_welfare","get_keys_share:cache_name not a string", tostring(cache_name))
		return {}
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_welfare","get_keys_share:cache_data not exist", tostring(cache_name))
		return {}
	end

	return cache_data:get_keys(max_count)
end


return FOO